package com.cssy.xxkj.logic.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cssy.authority.service.IScUserRolewhPlaceService;
import com.cssy.base.web.BaseController;
import com.cssy.base.web.BodyMsg;
import com.cssy.xxkj.basic.entity.BasicStorageInfoTestDO;
import com.cssy.xxkj.basic.service.IBasicStorageInfoTestService;
import com.cssy.xxkj.basic.service.IBasicUtilService;
import com.cssy.xxkj.logic.entity.LogicAllocatOutwhDO;
import com.cssy.xxkj.logic.entity.LogicAllocatOutwhItemDO;
import com.cssy.xxkj.logic.service.ILogicAllocatOutwhItemService;
import com.cssy.xxkj.logic.service.ILogicAllocatOutwhService;
import com.cssy.yd.logic.util.SqlUtil;
import com.plat.helper.JqGridHandler;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.GlobalResLoader;
import com.plat.util.OperationFlag;
import com.plat.util.json.OutJsonGrid;

/**
 * 调拨发起controller
 * @author zeyong.gui
 *
 */
@Controller
@RequestMapping("/logic/allocatoutwh")
public class AllocatOutwhController extends BaseController<LogicAllocatOutwhDO> {
	
	private static final String MANAGER = "/xxkj/logic/allocat/ManagerStart";
	private static final String EIDT = "/xxkj/logic/allocat/OperationStart";
	
	@Resource(name = "basicUtilService")
	IBasicUtilService basicUtilService;
	
	@Resource(name = "basicStorageInfoTestService")
	IBasicStorageInfoTestService basicStorageInfoTestService;
	@Resource(name = "logicAllocatOutwhService")
	ILogicAllocatOutwhService logicAllocatOutwhService;
	
	@Resource(name = "logicAllocatOutwhItemService")
	ILogicAllocatOutwhItemService logicAllocatOutwhItemService;
	
	@Resource(name="scUserRolePlaceService")
	private IScUserRolewhPlaceService scUserRolePlaceService;
	
	@Override
	public String manager(HttpServletRequest request, Model model)
			throws Exception {
		JqGridHandler hander = new JqGridHandler(request);
		SearchConditions scs = new SearchConditions();
		hander.getWheres(scs);
		scs.setRemovedProperty("removed");
		//入库仓
		List<BasicStorageInfoTestDO> list = basicUtilService.resultStorage(scs);
		model.addAttribute("wh",list);
		//出库仓
		model.addAttribute("wh1",SqlUtil.sqlUtil.roleWH(request, scUserRolePlaceService));
		return MANAGER;
	}

	@Override
	public void list(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		JqGridHandler hander = new JqGridHandler(request);
		SearchConditions scs = new SearchConditions();
		hander.getWheres(scs);
		SearchResult result = logicAllocatOutwhService.findListByExample(scs);
		OutJsonGrid out = new OutJsonGrid(result);
		out.printJSon(response);
		
	}

	@Override
	public String operationForm(HttpServletRequest request, @PathVariable String id,
			@PathVariable String flag, Model model) throws Exception {
		if (OperationFlag.ADD.equals(flag)) {
			model.addAttribute("flag", "ADD");
		} else {
			LogicAllocatOutwhDO actoutwh = logicAllocatOutwhService.findListById(id);
			model.addAttribute("actoutwh", actoutwh);
		}
		//查询仓库
		SearchConditions whScs = new SearchConditions();
		whScs.setRemovedProperty(null);
		List<BasicStorageInfoTestDO> whList = basicUtilService.resultStorage(whScs);
		model.addAttribute("whList", whList);
		return EIDT;
	}

	@Override
	public Map<String, Object> save(HttpServletRequest request,
			LogicAllocatOutwhDO curAP, BindingResult result, Model model)
			throws Exception {
		BodyMsg bodyMap = BodyMsg.getInstance();
		try{
			String dataItem = request.getParameter("dataItem");
			if(StringUtils.isBlank(dataItem)){
				bodyMap.isFail();
				bodyMap.configWebInfor("产品列表不能为空！");
				return bodyMap.modelMap;
			}
			logicAllocatOutwhService.allocatOutwhSave(request, curAP);
			bodyMap.isSuccess();
			bodyMap.configWebInfor(GlobalResLoader.getOpsuccess());
		}catch(Exception e){
			e.printStackTrace();
			bodyMap.isFail();
			bodyMap.configWebInfor(GlobalResLoader.getOpfailed()+ e.getMessage());
		}
		return bodyMap.modelMap;
	}

	@Override
	public Map<String, Object> delete(HttpServletRequest request,
			HttpServletResponse response, Model model) throws Exception {
		BodyMsg bodyMap = BodyMsg.getInstance();
		try {
			String ids = request.getParameter("ids");
			String[] fields=ids.split(",");
			for(String field : fields){
				if(StringUtils.isBlank(field)){
					continue;
				}else{
					logicAllocatOutwhService.deleteBizRemoved(field);
				}
			}
			bodyMap.isSuccess(GlobalResLoader.getOpsuccess());
		} catch (Exception e) {
			e.printStackTrace();
			bodyMap.isFail(GlobalResLoader.getOpfailed() + e.getMessage());
		}
		return bodyMap.modelMap;
	}

	/**
	 * 查询调拨发起单的行记录
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/gridItems")
	public void gridItems(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String actoutwhId = request.getParameter("actoutwhId");
		if(StringUtils.isNotBlank(actoutwhId)){
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("actoutwhId", actoutwhId);
			List<LogicAllocatOutwhItemDO> pcsrmList = logicAllocatOutwhItemService.findListByMap(paramMap);
			OutJsonGrid out = new OutJsonGrid(pcsrmList);
			out.printJSon(response);
		}
	}
}
